//
//  IndexTableCell.m
//  KangarooDoctor
//
//  Created by mengze on 2024/11/15.
//

#import <Foundation/Foundation.h>
#import "BlueDeviceItem.h"
#import "Masonry.h"

@interface BlueDeviceItem()

@property( nonatomic, strong ) UILabel * titleView;
@property( nonatomic, strong ) UIView * sepView;
@property( nonatomic, strong ) UIButton * bgButton;


@property( nonatomic, strong ) NSString * title;
@property( nonatomic, assign ) Boolean hasSep;
@property( nonatomic, assign ) NSInteger type;
@property( nonatomic, weak ) id<BtnTapDelegate> delegate;

@end


@implementation BlueDeviceItem


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    self.selectionStyle=UITableViewCellSelectionStyleBlue;
    if(self)
    {
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 12.0;

        [self initWithView];
        [self initWithData];
    }
    return self;
}


- ( void ) startWithTitle:(NSString *) title  row:(NSInteger) row delete: (id<BtnTapDelegate>) delegate {

    NSLog(@"title: %@", title);
    self.titleView.text = title;
    self.delegate = delegate;
    self.type = row;
    
}

- (void) initWithView {
    
    [self setBackgroundColor:UIColor.whiteColor];

    self.titleView = [[UILabel alloc] init];
    [self.titleView setText:self.title];
    [self.titleView setFont:[UIFont systemFontOfSize:16]];
    [self.contentView addSubview:self.titleView];
    
    
    if( self.hasSep ){
        self.sepView = [[UIView alloc] init];
        [self.contentView addSubview: self.sepView];
        [self.sepView setBackgroundColor:[UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1]];
    }
    
    self.bgButton = [UIButton buttonWithType:UIButtonTypeSystem];
    [self.bgButton setFrame:CGRectMake(250, 18, 100, 36)];
    [self.bgButton setTitle:@"开始连接" forState:UIControlStateNormal];
    [self.bgButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
    [self.bgButton setBackgroundColor:[UIColor greenColor]];
    [self.bgButton addTarget:self action:@selector(buttonDidTap:) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.bgButton];
    
    self.bgButton.layer.masksToBounds = TRUE;
    self.bgButton.layer.cornerRadius = 6;
    
}


- ( void ) buttonDidTap: (id) sender {
    
    NSLog(@"buttonDidTap........");
    [self.delegate btnDidClickWithType:self.type];
}


- (void) initWithData {
    
    [self.titleView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo( self );
        make.left.mas_equalTo( self ).offset(12);
        make.height.mas_equalTo(20);
    }];
    
    
    if( self.hasSep ){
        [self.sepView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self);
            make.height.mas_equalTo(1);
            make.left.mas_equalTo(self);
            make.right.mas_equalTo(self);
        }];
    }
  
    [self.bgButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo( self );
        make.right.mas_equalTo(self).offset(-12);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(32);
    }];
}




@end
